# -*- coding:utf-8 -*-
# @Author:Cheng Lei 1037654919@qq.com
# @Time : 2023/5/10 下午2:54
# @FileName: kuwo_t7_bang.py
# @Software: PyCharm
# 获取榜单
import pandas as pd
from tqdm import tqdm
from multiprocessing import Pool
from class_kuwo import Kuwo

kuwo = Kuwo()
from utils import MongoDBUtil, get_html, mongo_manager

MongoDBUtil = MongoDBUtil()
playlist_tag = mongo_manager("playlist_tag", db="kuwo")
playlist_song = mongo_manager("playlist_song", db="kuwo")
singer_song = mongo_manager("singer_song", db="kuwo")
playlist_song_list = mongo_manager("playlist_song_list", db="kuwo")
song_info = mongo_manager("song_info", db="kuwo")
bang_song_info = mongo_manager("bang_song_info", db="kuwo")


# 基于歌单更新歌曲列表
def playlist_song_list_run():
    seeds = playlist_song.findAll({"status": None})
    lists = []
    for seed in seeds:
        lists.append(seed)
    print('playlist_song_list None:', len(lists))
    for seed in seeds:
        result = {"_id": seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status'] = 'success'
        playlist_song.updateOne({'_id': seed['_id']}, seed)


# 基于歌手更新歌曲列表
def singer_song_list_run():
    seeds = singer_song.findAll({"status": None})
    lists = []
    # for seed in seeds:
    #     lists.append(seed)
    # print('singer_song_list_run None:',len(lists))
    for seed in seeds:
        result = {"_id": seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status'] = 'success'
        singer_song.updateOne({'_id': seed['_id']}, seed)


# 插入数据
def insert_data(tablename, datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


# 解析歌曲信息
def parse_song(data):
    datas = data
    result = {'_id': datas.get("rid"), 'musicrid': datas.get("musicrid"),
              'name': datas.get("name"), 'score100': datas.get("score100"),
              'img': datas.get("pic"), 'album': datas.get("album"),
              'albumid': datas.get("albumid"), 'albuminfo': datas.get("albuminfo"),
              'albumpic': datas.get("albumpic"), 'artist': datas.get("artist"),
              'artistid': datas.get("artistid"), 'barrage': datas.get("barrage"),
              'content_type': datas.get("content_type"), 'duration': datas.get("duration"),
              'hasLossless': datas.get("hasLossless"), 'hasmv': datas.get("hasmv"),
              'isListenFee': datas.get("isListenFee"), 'isstar': datas.get("isstar"),
              'mvPlayCnt': datas.get("mvPlayCnt"), 'mvUpPcStr': datas.get("mvUpPcStr"),
              'online': datas.get("online"), 'originalsongtype': datas.get("originalsongtype"),

              'releaseDate': datas.get("releaseDate"),
              'songTimeMinutes': datas.get("songTimeMinutes"), 'tme_musician_adtype': datas.get("tme_musician_adtype"),
              'track': datas.get("track"), 'upPcStr': datas.get("upPcStr")
              }
    return result


# 解析榜单信息
def parse_bang_list(date, bang_name, value, data):
    datass = data['musicList']
    results = []
    for datas in datass:
        result = {'_id': str(date) + '_' + str(value) + '_' + str(datas.get("rid")),
                  'bang_name': bang_name,
                  'bang_id': value,
                  'musicrid': datas.get("musicrid"),
                  'artist': datas.get("artist"),
                  'artistid': datas.get("artistid"),
                  'releaseDate': datas.get("releaseDate"),
                  'trend': datas.get("trend"),
                  'rid': datas.get("rid"),
                  'pic': datas.get("pic"),
                  'isstar': datas.get("isstar"),
                  'duration': datas.get("duration"),
                  'name': datas.get("name"),
                  'online': datas.get("online"),
                  'songTimeMinutes': datas.get("songTimeMinutes")
                  }
        results.append(result)
    return results


# 获取歌曲信息
def get_music_info(seed):
    playlist_song_list = mongo_manager("playlist_song_list", db="kuwo")
    song_info = mongo_manager("song_info", db="kuwo")
    mid = seed['_id']
    datas = kuwo.get_music_info(mid=mid)
    results = parse_song(datas)
    lyric = kuwo.get_song_lyric(mid=mid)  # list
    results['lyric'] = lyric
    try:
        song_info.insertOne(results)
        print("插入成功数据:", results["_id"])
    except Exception as e:
        print("error:", e)
    # insert_data(tablename='song_info', datas=[results])
    seed['status'] = 'success'
    playlist_song_list.updateOne({'_id': seed['_id']}, seed)
    playlist_song_list.close()
    song_info.close()
    # return seeds


# 获取歌曲信息 run
def get_bangs(key):
    value = key[1]
    bang_name = key[0]
    # testd = {'酷我飙升榜':93}
    # for key,value in testd.items():
    print(bang_name, value)
    data = kuwo.get_bang(bangId=value)
    # print(data)
    num = data.get("num")
    pub = data.get("pub")
    img = data.get("img")

    print(num)
    print(data)
    results = []
    results2 = []

    for page in range(1, int(int(num) / 20) + 1):
        # for page in range(1, 3):
        print('page:', page)
        data = kuwo.get_bang(bangId=value, pn=page)
        musicList = data.get("musicList")
        print(len(musicList))
        results += parse_bang_list(pub, bang_name, value, data)
        insert_data(tablename='bang_song_list', datas=results)

    for result in results:
        print(result['rid'])
        mid = result['rid']
        datas = kuwo.get_music_info(mid=mid)
        # songs = {'date': pub,'bang_name': bang_name,'bang_id': value}
        songs = {}
        songs.update(parse_song(datas))
        lyric = kuwo.get_song_lyric(mid=mid)  # list
        songs['lyric'] = lyric
        # insert_data(tablename='bang_song_info', datas=[songs])
        results2.append(songs)

    # df = pd.DataFrame(results2,columns=['_id', 'musicrid','name', 'score100','img', 'album','albumid', 'albuminfo','albumpic','artist','artistid',
    #                                     'barrage','content_type', 'duration','hasLossless', 'hasmv','isListenFee', 'isstar','mvPlayCnt', 'mvUpPcStr',
    #                                     'online', 'originalsongtype','releaseDate',
    #                                     'songTimeMinutes', 'tme_musician_adtype','track', 'upPcStr','lyric'])
    # df.to_excel('bang_music_info_{}.xlsx'.format(bang_name))

    # df = pd.DataFrame(results,columns=['_id','musicrid','artist','artistid','releaseDate',
    #                               'trend','rid','pic','isstar','duration','name',
    #                               'online','songTimeMinutes'])
    # df.to_excel('test_bang.xlsx')


def test_qushu():
    lists = ['周杰伦', 'G.E.M.邓紫棋', '林俊杰', '陈奕迅', '张学友', '薛之谦', 'Beyond', '张杰', '王菲', '刘德华',
             '赵雷', '周传雄', '许巍', '任贤齐', '李克勤', '张信哲', '五月天', '李宗盛', '王心凌', '蔡依林', '李玉刚',
             '朴树', '杨千嬅', '齐秦', '潘玮柏', '林志炫', '胡彦斌', '陶喆', '汪峰', '孙燕姿', '庾澄庆', '许嵩',
             '陈小春', '蔡健雅', '林子祥', '郭顶', '郑智化', '萧亚轩', 'Justin Bieber', '李宇春', '伍佰', '古巨基',
             '杨坤', '徐良', '李圣杰', '郑伊健', '周深', '毛不易', '莫文蔚', '程响', '陈慧娴', '郁可唯', 'Alan Walker',
             'Avicii', '王力宏', '金志文', '谢霆锋', '张敬轩', '华晨宇', '张震岳', '罗大佑', '吴青峰', '周柏豪', '那英',
             '孙楠', '草蜢', '胡夏', '陈粒', '张惠妹', '郑少秋', '马頔', '卢巧音', '黑豹乐队', '陈柏宇', '吴克群',
             '江语晨', '王嘉尔', '任嘉伦', '苏打绿', 'Lil Nas X', 'Taylor Swift Tribute Band', '温岚', 'By2', '萧敬腾',
             '张国荣', '张靓颖', '庄心妍', '杨宗纬', '刘欢', '马天宇', '谭咏麟', '窦唯', 'S.H.E', '张雨生', '方大同',
             '尚雯婕', '张宇', 'Michael Jakson', 'Olly Murs', 'Martin Garrix', 'Sia', 'Kelly Clarkson', 'Billie Eilish',
             'P!nk', 'LiSA', 'HENRY刘宪华', '朴灿烈', '坂本龍一', '米津玄师', '筷子兄弟', '逃跑计划', '张碧晨',
             '刘若英', '张韶涵', '王靖雯', '周华健', '刀郎', '韩红', '满舒克', '梁静茹', '胡海泉', '陈势安', '许冠杰',
             '张楚生', '袁娅维TIA RAY', '田馥甄', '郑秀文', '杨培安', '信乐队', 'GALA', '焦迈奇', '艾福杰尼',
             '吉克隽逸', '戴佩妮', '张艺兴', '鹿晗', '周兴哲', '蔡徐坤', '老狼', '花粥', '郝云', '毛宁', '王若琳',
             '汪苏泷', '陈绮贞', '姜育恒', 'MISSKO', '容祖儿', '阿杜', '郑钧', '风凰传奇', '热狗', 'CoCo李玟', '大冢爱',
             '庞龙', 'Imagine Dragons', 'Maroon 5', 'Westlife', 'Linkin Park', 'Professor Green ', 'Ed Sheeran',
             'Charlie Puth', 'G-Dragon', 'Adele', 'Madonna', 'Rihanna', 'Avril Lavigne', '曲婉婷', '黎明', '卢冠廷',
             '范玮琪', '谭维维', '马吟吟', '张栋梁', '张智霖', '卫兰', '田震', '郭富城', '李玖哲', '丁当', '腾格尔',
             '宇多田光', '詹雯婷', '青鸟飞鱼', '黄小琥', '巫启贤', '韦礼安', '邱永传', '品冠', '阿兰', '齐一', '曹格',
             '蔡国庆', '龚琳娜', '方力申', '乌兰', '卓文萱', '曲旦卓玛', 'Lady Gaga', 'The Score',
             '许魏洲', '余佳运', 'Shawn Mendes', '阿冗', '木小雅', '比莉', '吴雨霏', '王贰浪']
    songlist = []
    ii = 0
    for artist in tqdm(lists[:]):
        ii += 1
        seeds = song_info.findAll({"artist": artist})
        lens = 0
        for s in seeds:
            lens += 1
            songlist.append(s)
            if lens >= 60:
                break
        print(artist, lens)
        # print(artist, lens)
        if len(songlist) > 15000:
            print(artist)
            pf = pd.DataFrame(songlist,
                              columns=['_id', 'musicrid', 'name', 'score100', 'img', 'album', 'albumid', 'albuminfo',
                                       'albumpic', 'artist', 'artistid', 'barrage', 'content_type', 'duration',
                                       'hasLossless', 'hasmv', 'isListenFee', 'isstar', 'mvPlayCnt', 'mvUpPcStr',
                                       'online', 'originalsongtype', 'releaseDate',
                                       'songTimeMinutes', 'tme_musician_adtype', 'track', 'upPcStr', 'lyric'])
            pf.to_excel('qushu_test_{}.xlsx'.format(ii))
            songlist = []
    pf = pd.DataFrame(songlist, columns=['_id', 'musicrid', 'name', 'score100', 'img', 'album', 'albumid', 'albuminfo',
                                         'albumpic', 'artist', 'artistid', 'barrage', 'content_type', 'duration',
                                         'hasLossless', 'hasmv', 'isListenFee', 'isstar', 'mvPlayCnt', 'mvUpPcStr',
                                         'online', 'originalsongtype', 'releaseDate',
                                         'songTimeMinutes', 'tme_musician_adtype', 'track', 'upPcStr', 'lyric'])
    pf.to_excel('qushu_test.xlsx')
    pass


def test_qushu1():
    seeds = bang_song_info.findAll()
    lists = []
    for seed in seeds:
        lists.append(seed)
    df = pd.DataFrame(lists, columns=['_id', 'musicrid', 'name', 'score100', 'img', 'album', 'albumid', 'albuminfo',
                                      'albumpic', 'artist', 'artistid',
                                      'barrage', 'content_type', 'duration', 'hasLossless', 'hasmv', 'isListenFee',
                                      'isstar', 'mvPlayCnt', 'mvUpPcStr',
                                      'online', 'originalsongtype', 'releaseDate',
                                      'songTimeMinutes', 'tme_musician_adtype', 'track', 'upPcStr', 'lyric'])
    # df.to_excel('bang_music_info_{}.xlsx'.format(bang_name))
    df.to_excel('bang_song_info.xlsx')
    pass


if __name__ == '__main__':
    bangIds = {'酷我飙升榜': 93, '酷我新歌榜': 17, '酷我热歌榜': 16, '抖音歌曲榜': 158,
               '万物DJ榜': 176, '会员畅听榜': 145, '会员飙升榜': 336, '会员爱听排行榜': 331,
               '会员新歌榜': 330, '极品电音榜': 242, '酷我热评榜': 284, '经典怀旧榜': 26,
               '古风音乐榜': 278, '网红新歌榜': 153, '流行趋势榜': 187, '最强翻唱榜': 185,
               '酷我综艺榜': 154, '酷我说唱榜': 329, '影视金曲榜': 64, '禅心佛乐榜': 294,
               'ACG新歌榜': 290, '酷我铃声榜': 292, '跑步健身榜': 297,
               '春日温柔榜': 279, '车载歌曲榜': 328, '爆笑相声榜': 291,
               '宝宝哄睡榜': 295, '睡前放松榜': 283, '熬夜修仙榜': 282,
               'Vlog': 264, 'KTV': 255, '通勤路上榜': 281
               }
    # run
    # pool = Pool(processes=40)
    # pool.map(get_bangs, bangIds.items())
    # pool.close()  # 关闭进程池，不再接受新的进程
    # pool.join()

    # 下载榜单信息test
    # get_bangs(['酷我飙升榜',93])

    # 临时取数
    # test_qushu()
    test_qushu1()

    pass
